package nc.impl.bd.defdoc;

import java.util.List;

import nc.bs.framework.common.RuntimeEnv;
import nc.bs.logging.Logger;
import nc.itf.bd.defdoc.DefdocUIUtil;
import nc.md.model.IAssociation;
import nc.md.model.IAttribute;
import nc.md.model.IBean;
import nc.md.model.IBusiOperation;
import nc.md.model.IComponent;
import nc.ui.pub.codegen.VelocityUnSingletonCodeGenerator;
import nc.ui.pub.codegen.exception.CodegenException;
import nc.vo.bd.defdoc.DefdocBpfVmObject;
import nc.vo.bd.defdoc.DefdocPublishVmObject;
import nc.vo.bd.defdoc.Defdoclist2ResIDUtil;
import nc.vo.bd.defdoc.DefdoclistVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.guid.UUIDGenerator;
import nc.vo.pub.lang.UFDateTime;
import nccloud.commons.lang.StringUtils;

public class DefdoclistMDFileGenerator
{
  private static final String VM_PATH = "/resources/nc/ui/bbd/defdoc/";
  private static final String VM_BPFFILE = "DefdocBpfPublishTemplate.vm";
  private static final String VM_LISTFILE = "ListDefdocPublishTemplate.vm";
  private static final String VM_TREEFILE = "TreeDefdocPublishTemplate.vm";
  public static final String METADATA_BPF_PATH = "/modules/uapbd/METADATA/metadata/udi_bpf/";
  public static final String METADATA_PATH = "/modules/uapbd/METADATA/metadata/udi/";

  public static String getDefdocBmfFileName(DefdoclistVO defdoclist)
  {
    String nchome = RuntimeEnv.getInstance().getNCHome();
    StringBuilder destPath = new StringBuilder(nchome);
    destPath.append("/modules/uapbd/METADATA/metadata/udi/");
    destPath.append("Defdoc-");
    destPath.append(defdoclist.getCode());
    destPath.append(".bmf");
    return destPath.toString();
  }

  public static String generateBmfFile(IBean bean, DefdoclistVO defdoclist) throws BusinessException
  {
    DefdocPublishVmObject vmObj = createVmObject(bean, defdoclist);

    return generateBmfFile(defdoclist, vmObj);
  }

  private static DefdocPublishVmObject createVmObject(IBean bean, DefdoclistVO defdoclist) {
    DefdocPublishVmObject vmObj = new DefdocPublishVmObject();
    vmObj.setClassID(defdoclist.getPrimaryKey());
    vmObj.setDisplayName(defdoclist.getName() + DefdocUIUtil.getDefdocNameSuff());
    vmObj.setEntityAsRefName(defdoclist.getName() + DefdocUIUtil.getDefdocNameSuff());
    vmObj.setPidRefModeName(vmObj.getEntityAsRefName());
    vmObj.setName("Defdoc-" + defdoclist.getCode());
    vmObj.setTableName(defdoclist.getAssociatename());
    vmObj.setEntityResid(Defdoclist2ResIDUtil.getResidByDefdoclistName(defdoclist.getName()));
    if (bean == null) {
      vmObj.setBusiItfConnectionID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setCodeID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      if (StringUtils.isNotEmpty(defdoclist.getComponentid()))
        vmObj.setComponentID(defdoclist.getComponentid());
      else {
        vmObj.setComponentID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      }
      vmObj.setCreaterRelationID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setCreateTime(new UFDateTime(System.currentTimeMillis()).toString());
      vmObj.setCreateTimeID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setCreateUserID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setGroupRelationID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setInnerCodeID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setKeyAttributeID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setMemoID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setMnecodeID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setModifierID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setModifierRelationID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setModifyTime("");
      vmObj.setModifyTimeID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setNameID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setShortnameID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setOrgRelationID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setPidRefModeName(defdoclist.getName() + DefdocUIUtil.getDefdocNameSuff());
      vmObj.setParentID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setParentRelationID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setPkAttriID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setPkGroupID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setPkOrgID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setEnablestateID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setSelfPkRelationID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDataoriginflagID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDatatypeID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setVersion("1");
      vmObj.setDef1(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef2(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef3(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef4(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef5(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef6(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef7(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef8(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef9(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef10(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef11(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef12(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef13(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef14(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef15(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef16(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef17(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef18(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef19(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef20(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef21(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef22(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef23(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef24(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef25(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef26(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef27(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef28(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef29(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef30(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef31(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef32(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef33(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef34(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef35(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef36(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef37(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef38(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef39(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef40(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef41(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef42(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef43(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef44(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef45(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef46(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef47(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef48(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef49(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef50(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef51(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef52(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef53(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef54(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef55(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef56(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef57(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef58(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef59(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef60(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef61(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef62(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef63(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef64(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef65(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef66(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef67(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef68(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef69(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef70(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef71(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef72(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef73(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef74(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef75(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef76(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef77(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef78(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef79(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef80(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef81(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef82(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef83(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef84(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef85(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef86(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef87(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef88(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef89(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef90(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef91(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef92(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef93(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef94(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef95(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef96(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef97(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef98(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef99(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef100(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef101(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef102(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef103(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef104(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef105(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef106(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef107(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef108(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef109(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef110(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef111(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef112(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef113(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef114(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef115(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef116(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef117(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef118(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef119(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef120(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef121(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef122(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef123(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef124(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef125(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef126(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef127(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef128(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef129(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef130(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef131(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef132(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef133(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef134(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef135(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef136(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef137(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef138(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef139(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef140(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef141(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef142(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef143(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef144(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef145(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef146(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef147(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef148(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef149(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef150(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef151(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef152(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef153(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef154(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef155(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef156(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef157(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef158(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef159(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef160(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef161(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef162(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef163(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef164(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef165(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef166(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef167(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef168(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef169(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef170(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef171(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef172(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef173(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef174(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef175(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef176(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef177(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef178(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef179(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef180(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef181(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef182(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef183(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef184(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef185(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef186(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef187(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef188(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef189(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef190(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef191(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef192(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef193(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef194(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef195(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef196(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef197(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef198(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef199(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDef200(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());

    } else {
      List<IAssociation> associations = bean.getAssociations();
      for (IAssociation association : associations) {
        String target = association.getEndElement().getAssElement().getID();
        if ("6c8722b9-911a-489b-8d0d-18bd3734fcf6".equals(target))
          vmObj.setBusiItfConnectionID(association.getID());
        else if ("f6f9a473-56c0-432f-8bc7-fbf8fde54fee".equals(target)) {
          if (association.getStartAttribute().getName().equals("creator"))
            vmObj.setCreaterRelationID(association.getID());
          else if (association.getStartAttribute().getName().equals("modifier"))
            vmObj.setModifierRelationID(association.getID());
        }
        else if ("3b6dd171-2900-47f3-bfbe-41e4483a2a65".equals(target))
          vmObj.setGroupRelationID(association.getID());
        else if ("985be8a4-3a36-4778-8afe-2d8ed3902659".equals(target))
          vmObj.setOrgRelationID(association.getID());
        else if ("16c1dca0-e3ed-4dcc-8ed7-75fd59e2bef1".equals(target))
          vmObj.setSelfPkRelationID(association.getID());
        else if (bean.getID().equals(target)) {
          vmObj.setParentRelationID(association.getID());
        }
      }

      if (vmObj.getBusiItfConnectionID() == null) {
        vmObj.setBusiItfConnectionID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      }

      vmObj.setCodeID(bean.getAttributeByName("code").getID());
      vmObj.setComponentID(bean.getContainer().getID());
      vmObj.setCreateTime(defdoclist
        .getCreationtime() == null ? new UFDateTime(System.currentTimeMillis()).toLocalString() : defdoclist
        .getCreationtime().toString());
      vmObj.setCreateTimeID(bean.getAttributeByName("creationtime").getID());
      vmObj.setCreateUserID(bean.getAttributeByName("creator").getID());
      vmObj.setInnerCodeID(bean.getAttributeByName("innercode").getID());
      vmObj.setKeyAttributeID(bean.getAttributeByName("pk_defdoc").getID());
      vmObj.setMemoID(bean.getAttributeByName("memo").getID());
      vmObj.setMnecodeID(bean.getAttributeByName("mnecode").getID());
      vmObj.setModifierID(bean.getAttributeByName("modifier").getID());
      vmObj.setModifyTime(new UFDateTime(System.currentTimeMillis()).toString());
      vmObj.setModifyTimeID(bean.getAttributeByName("modifiedtime").getID());
      vmObj.setNameID(bean.getAttributeByName("name").getID());
      vmObj.setShortnameID(bean.getAttributeByName("shortname").getID());
      vmObj.setParentID(bean.getAttributeByName("pid").getID());
      vmObj.setPkAttriID(bean.getAttributeByName("pk_defdoclist").getID());
      vmObj.setPkGroupID(bean.getAttributeByName("pk_group").getID());
      vmObj.setPkOrgID(bean.getAttributeByName("pk_org").getID());
      vmObj.setEnablestateID(bean.getAttributeByName("enablestate").getID());
      vmObj.setDataoriginflagID(bean.getAttributeByName("dataoriginflag").getID());
      vmObj.setDatatypeID(bean.getAttributeByName("datatype").getID());

      String oldVersion = ((IComponent)bean.getContainer()).getVersion();
      vmObj.setVersion(String.valueOf(Integer.valueOf(oldVersion == null ? "1" : oldVersion).intValue() + 1));

      vmObj.setDef1(getDefAttrId(bean, "def1"));
      vmObj.setDef2(getDefAttrId(bean, "def2"));
      vmObj.setDef3(getDefAttrId(bean, "def3"));
      vmObj.setDef4(getDefAttrId(bean, "def4"));
      vmObj.setDef5(getDefAttrId(bean, "def5"));
      vmObj.setDef6(getDefAttrId(bean, "def6"));
      vmObj.setDef7(getDefAttrId(bean, "def7"));
      vmObj.setDef8(getDefAttrId(bean, "def8"));
      vmObj.setDef9(getDefAttrId(bean, "def9"));
      vmObj.setDef10(getDefAttrId(bean, "def10"));
      vmObj.setDef11(getDefAttrId(bean, "def11"));
      vmObj.setDef12(getDefAttrId(bean, "def12"));
      vmObj.setDef13(getDefAttrId(bean, "def13"));
      vmObj.setDef14(getDefAttrId(bean, "def14"));
      vmObj.setDef15(getDefAttrId(bean, "def15"));
      vmObj.setDef16(getDefAttrId(bean, "def16"));
      vmObj.setDef17(getDefAttrId(bean, "def17"));
      vmObj.setDef18(getDefAttrId(bean, "def18"));
      vmObj.setDef19(getDefAttrId(bean, "def19"));
      vmObj.setDef20(getDefAttrId(bean, "def20"));
      vmObj.setDef21(getDefAttrId(bean, "def21"));
      vmObj.setDef22(getDefAttrId(bean, "def22"));
      vmObj.setDef23(getDefAttrId(bean, "def23"));
      vmObj.setDef24(getDefAttrId(bean, "def24"));
      vmObj.setDef25(getDefAttrId(bean, "def25"));
      vmObj.setDef26(getDefAttrId(bean, "def26"));
      vmObj.setDef27(getDefAttrId(bean, "def27"));
      vmObj.setDef28(getDefAttrId(bean, "def28"));
      vmObj.setDef29(getDefAttrId(bean, "def29"));
      vmObj.setDef30(getDefAttrId(bean, "def30"));
      vmObj.setDef31(getDefAttrId(bean, "def31"));
      vmObj.setDef32(getDefAttrId(bean, "def32"));
      vmObj.setDef33(getDefAttrId(bean, "def33"));
      vmObj.setDef34(getDefAttrId(bean, "def34"));
      vmObj.setDef35(getDefAttrId(bean, "def35"));
      vmObj.setDef36(getDefAttrId(bean, "def36"));
      vmObj.setDef37(getDefAttrId(bean, "def37"));
      vmObj.setDef38(getDefAttrId(bean, "def38"));
      vmObj.setDef39(getDefAttrId(bean, "def39"));
      vmObj.setDef40(getDefAttrId(bean, "def40"));
      vmObj.setDef41(getDefAttrId(bean, "def41"));
      vmObj.setDef42(getDefAttrId(bean, "def42"));
      vmObj.setDef43(getDefAttrId(bean, "def43"));
      vmObj.setDef44(getDefAttrId(bean, "def44"));
      vmObj.setDef45(getDefAttrId(bean, "def45"));
      vmObj.setDef46(getDefAttrId(bean, "def46"));
      vmObj.setDef47(getDefAttrId(bean, "def47"));
      vmObj.setDef48(getDefAttrId(bean, "def48"));
      vmObj.setDef49(getDefAttrId(bean, "def49"));
      vmObj.setDef50(getDefAttrId(bean, "def50"));
      vmObj.setDef51(getDefAttrId(bean, "def51"));
      vmObj.setDef52(getDefAttrId(bean, "def52"));
      vmObj.setDef53(getDefAttrId(bean, "def53"));
      vmObj.setDef54(getDefAttrId(bean, "def54"));
      vmObj.setDef55(getDefAttrId(bean, "def55"));
      vmObj.setDef56(getDefAttrId(bean, "def56"));
      vmObj.setDef57(getDefAttrId(bean, "def57"));
      vmObj.setDef58(getDefAttrId(bean, "def58"));
      vmObj.setDef59(getDefAttrId(bean, "def59"));
      vmObj.setDef60(getDefAttrId(bean, "def60"));
      vmObj.setDef61(getDefAttrId(bean, "def61"));
      vmObj.setDef62(getDefAttrId(bean, "def62"));
      vmObj.setDef63(getDefAttrId(bean, "def63"));
      vmObj.setDef64(getDefAttrId(bean, "def64"));
      vmObj.setDef65(getDefAttrId(bean, "def65"));
      vmObj.setDef66(getDefAttrId(bean, "def66"));
      vmObj.setDef67(getDefAttrId(bean, "def67"));
      vmObj.setDef68(getDefAttrId(bean, "def68"));
      vmObj.setDef69(getDefAttrId(bean, "def69"));
      vmObj.setDef70(getDefAttrId(bean, "def70"));
      vmObj.setDef71(getDefAttrId(bean, "def71"));
      vmObj.setDef72(getDefAttrId(bean, "def72"));
      vmObj.setDef73(getDefAttrId(bean, "def73"));
      vmObj.setDef74(getDefAttrId(bean, "def74"));
      vmObj.setDef75(getDefAttrId(bean, "def75"));
      vmObj.setDef76(getDefAttrId(bean, "def76"));
      vmObj.setDef77(getDefAttrId(bean, "def77"));
      vmObj.setDef78(getDefAttrId(bean, "def78"));
      vmObj.setDef79(getDefAttrId(bean, "def79"));
      vmObj.setDef80(getDefAttrId(bean, "def80"));
      vmObj.setDef81(getDefAttrId(bean, "def81"));
      vmObj.setDef82(getDefAttrId(bean, "def82"));
      vmObj.setDef83(getDefAttrId(bean, "def83"));
      vmObj.setDef84(getDefAttrId(bean, "def84"));
      vmObj.setDef85(getDefAttrId(bean, "def85"));
      vmObj.setDef86(getDefAttrId(bean, "def86"));
      vmObj.setDef87(getDefAttrId(bean, "def87"));
      vmObj.setDef88(getDefAttrId(bean, "def88"));
      vmObj.setDef89(getDefAttrId(bean, "def89"));
      vmObj.setDef90(getDefAttrId(bean, "def90"));
      vmObj.setDef91(getDefAttrId(bean, "def91"));
      vmObj.setDef92(getDefAttrId(bean, "def92"));
      vmObj.setDef93(getDefAttrId(bean, "def93"));
      vmObj.setDef94(getDefAttrId(bean, "def94"));
      vmObj.setDef95(getDefAttrId(bean, "def95"));
      vmObj.setDef96(getDefAttrId(bean, "def96"));
      vmObj.setDef97(getDefAttrId(bean, "def97"));
      vmObj.setDef98(getDefAttrId(bean, "def98"));
      vmObj.setDef99(getDefAttrId(bean, "def99"));
      vmObj.setDef100(getDefAttrId(bean, "def100"));
      vmObj.setDef101(getDefAttrId(bean, "def101"));
      vmObj.setDef102(getDefAttrId(bean, "def102"));
      vmObj.setDef103(getDefAttrId(bean, "def103"));
      vmObj.setDef104(getDefAttrId(bean, "def104"));
      vmObj.setDef105(getDefAttrId(bean, "def105"));
      vmObj.setDef106(getDefAttrId(bean, "def106"));
      vmObj.setDef107(getDefAttrId(bean, "def107"));
      vmObj.setDef108(getDefAttrId(bean, "def108"));
      vmObj.setDef109(getDefAttrId(bean, "def109"));
      vmObj.setDef110(getDefAttrId(bean, "def110"));
      vmObj.setDef111(getDefAttrId(bean, "def111"));
      vmObj.setDef112(getDefAttrId(bean, "def112"));
      vmObj.setDef113(getDefAttrId(bean, "def113"));
      vmObj.setDef114(getDefAttrId(bean, "def114"));
      vmObj.setDef115(getDefAttrId(bean, "def115"));
      vmObj.setDef116(getDefAttrId(bean, "def116"));
      vmObj.setDef117(getDefAttrId(bean, "def117"));
      vmObj.setDef118(getDefAttrId(bean, "def118"));
      vmObj.setDef119(getDefAttrId(bean, "def119"));
      vmObj.setDef120(getDefAttrId(bean, "def120"));
      vmObj.setDef121(getDefAttrId(bean, "def121"));
      vmObj.setDef122(getDefAttrId(bean, "def122"));
      vmObj.setDef123(getDefAttrId(bean, "def123"));
      vmObj.setDef124(getDefAttrId(bean, "def124"));
      vmObj.setDef125(getDefAttrId(bean, "def125"));
      vmObj.setDef126(getDefAttrId(bean, "def126"));
      vmObj.setDef127(getDefAttrId(bean, "def127"));
      vmObj.setDef128(getDefAttrId(bean, "def128"));
      vmObj.setDef129(getDefAttrId(bean, "def129"));
      vmObj.setDef130(getDefAttrId(bean, "def130"));
      vmObj.setDef131(getDefAttrId(bean, "def131"));
      vmObj.setDef132(getDefAttrId(bean, "def132"));
      vmObj.setDef133(getDefAttrId(bean, "def133"));
      vmObj.setDef134(getDefAttrId(bean, "def134"));
      vmObj.setDef135(getDefAttrId(bean, "def135"));
      vmObj.setDef136(getDefAttrId(bean, "def136"));
      vmObj.setDef137(getDefAttrId(bean, "def137"));
      vmObj.setDef138(getDefAttrId(bean, "def138"));
      vmObj.setDef139(getDefAttrId(bean, "def139"));
      vmObj.setDef140(getDefAttrId(bean, "def140"));
      vmObj.setDef141(getDefAttrId(bean, "def141"));
      vmObj.setDef142(getDefAttrId(bean, "def142"));
      vmObj.setDef143(getDefAttrId(bean, "def143"));
      vmObj.setDef144(getDefAttrId(bean, "def144"));
      vmObj.setDef145(getDefAttrId(bean, "def145"));
      vmObj.setDef146(getDefAttrId(bean, "def146"));
      vmObj.setDef147(getDefAttrId(bean, "def147"));
      vmObj.setDef148(getDefAttrId(bean, "def148"));
      vmObj.setDef149(getDefAttrId(bean, "def149"));
      vmObj.setDef150(getDefAttrId(bean, "def150"));
      vmObj.setDef151(getDefAttrId(bean, "def151"));
      vmObj.setDef152(getDefAttrId(bean, "def152"));
      vmObj.setDef153(getDefAttrId(bean, "def153"));
      vmObj.setDef154(getDefAttrId(bean, "def154"));
      vmObj.setDef155(getDefAttrId(bean, "def155"));
      vmObj.setDef156(getDefAttrId(bean, "def156"));
      vmObj.setDef157(getDefAttrId(bean, "def157"));
      vmObj.setDef158(getDefAttrId(bean, "def158"));
      vmObj.setDef159(getDefAttrId(bean, "def159"));
      vmObj.setDef160(getDefAttrId(bean, "def160"));
      vmObj.setDef161(getDefAttrId(bean, "def161"));
      vmObj.setDef162(getDefAttrId(bean, "def162"));
      vmObj.setDef163(getDefAttrId(bean, "def163"));
      vmObj.setDef164(getDefAttrId(bean, "def164"));
      vmObj.setDef165(getDefAttrId(bean, "def165"));
      vmObj.setDef166(getDefAttrId(bean, "def166"));
      vmObj.setDef167(getDefAttrId(bean, "def167"));
      vmObj.setDef168(getDefAttrId(bean, "def168"));
      vmObj.setDef169(getDefAttrId(bean, "def169"));
      vmObj.setDef170(getDefAttrId(bean, "def170"));
      vmObj.setDef171(getDefAttrId(bean, "def171"));
      vmObj.setDef172(getDefAttrId(bean, "def172"));
      vmObj.setDef173(getDefAttrId(bean, "def173"));
      vmObj.setDef174(getDefAttrId(bean, "def174"));
      vmObj.setDef175(getDefAttrId(bean, "def175"));
      vmObj.setDef176(getDefAttrId(bean, "def176"));
      vmObj.setDef177(getDefAttrId(bean, "def177"));
      vmObj.setDef178(getDefAttrId(bean, "def178"));
      vmObj.setDef179(getDefAttrId(bean, "def179"));
      vmObj.setDef180(getDefAttrId(bean, "def180"));
      vmObj.setDef181(getDefAttrId(bean, "def181"));
      vmObj.setDef182(getDefAttrId(bean, "def182"));
      vmObj.setDef183(getDefAttrId(bean, "def183"));
      vmObj.setDef184(getDefAttrId(bean, "def184"));
      vmObj.setDef185(getDefAttrId(bean, "def185"));
      vmObj.setDef186(getDefAttrId(bean, "def186"));
      vmObj.setDef187(getDefAttrId(bean, "def187"));
      vmObj.setDef188(getDefAttrId(bean, "def188"));
      vmObj.setDef189(getDefAttrId(bean, "def189"));
      vmObj.setDef190(getDefAttrId(bean, "def190"));
      vmObj.setDef191(getDefAttrId(bean, "def191"));
      vmObj.setDef192(getDefAttrId(bean, "def192"));
      vmObj.setDef193(getDefAttrId(bean, "def193"));
      vmObj.setDef194(getDefAttrId(bean, "def194"));
      vmObj.setDef195(getDefAttrId(bean, "def195"));
      vmObj.setDef196(getDefAttrId(bean, "def196"));
      vmObj.setDef197(getDefAttrId(bean, "def197"));
      vmObj.setDef198(getDefAttrId(bean, "def198"));
      vmObj.setDef199(getDefAttrId(bean, "def199"));
      vmObj.setDef200(getDefAttrId(bean, "def200"));

    }
    defdoclist.setComponentid(vmObj.getComponentID());
    return vmObj;
  }

  private static String getDefAttrId(IBean bean, String defFieldName) {
    String attrId = null;
    if (bean != null) {
      IAttribute attr = bean.getAttributeByName(defFieldName);
      if (attr != null) {
        attrId = attr.getID();
      }
    }
    if (attrId == null) {
      attrId = UUIDGenerator.getInstance().generateTimeBasedUUID().toString();
    }
    return attrId;
  }

  private static String generateBmfFile(DefdoclistVO defdoclist, DefdocPublishVmObject vmObj)
    throws BusinessException
  {
    String nchome = RuntimeEnv.getInstance().getNCHome();
    VelocityUnSingletonCodeGenerator generator = new VelocityUnSingletonCodeGenerator(nchome + "/resources/nc/ui/bbd/defdoc/");

    String destPath = getDefdocBmfFileName(defdoclist);

    if (defdoclist.getDoclevel().intValue() == 1)
      return null;
    try
    {
      if (defdoclist.getIsgrade().booleanValue())
        generator.generate(destPath, "TreeDefdocPublishTemplate.vm", vmObj, "UTF-8");
      else
        generator.generate(destPath, "ListDefdocPublishTemplate.vm", vmObj, "UTF-8");
    }
    catch (CodegenException e)
    {
      Logger.error(e.getMessage());
      throw new BusinessException(e.getMessage(), e);
    }
    return destPath.toString();
  }

  public static String getDefdocBpfFileName(DefdoclistVO defdoclist) {
    String nchome = RuntimeEnv.getInstance().getNCHome();
    StringBuilder destPath = new StringBuilder(nchome);
    destPath.append("/modules/uapbd/METADATA/metadata/udi_bpf/");
    destPath.append("bo_");
    destPath.append("Defdoc-");
    destPath.append(defdoclist.getCode());
    destPath.append(".bpf");
    return destPath.toString();
  }

  public static String generateBpfFile(DefdoclistVO defdoclist, IComponent busiComponent) throws BusinessException
  {
    DefdocBpfVmObject boVmObj = createBOVmObject(busiComponent, defdoclist);
    return generateBpfFile(defdoclist, boVmObj);
  }

  private static DefdocBpfVmObject createBOVmObject(IComponent busiComponent, DefdoclistVO defdoclist)
    throws BusinessException
  {
    DefdocBpfVmObject vmObj = new DefdocBpfVmObject();
    vmObj.setComponentDisplayName(defdoclist
      .getName() + DefdocUIUtil.getDefdocNameSuff() + DefdocUIUtil.getDefdocBusiOperatorSuff());
    vmObj.setComponentName("bo_Defdoc-" + defdoclist
      .getCode());
    vmObj.setModifyTime(new UFDateTime(System.currentTimeMillis()).toString());
    vmObj.setRelationEntityDisplayName(defdoclist.getName() + DefdocUIUtil.getDefdocNameSuff());
    vmObj.setRelationEntityID(defdoclist.getPrimaryKey());
    vmObj.setRelationEntityName("Defdoc-" + defdoclist.getCode());
    if (busiComponent == null)
    {
      if (StringUtils.isNotEmpty(defdoclist.getBpfcomponentid()))
        vmObj.setComponentID(defdoclist.getBpfcomponentid());
      else {
        vmObj.setComponentID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      }
      vmObj.setCreateTime(new UFDateTime(System.currentTimeMillis()).toString());
      vmObj.setAddOperateID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDeleteOperateID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setDisableOperateID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setEditOperatID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setEnableOperateID(UUIDGenerator.getInstance().generateTimeBasedUUID().toString());
      vmObj.setVersion("1");
    } else {
      vmObj.setComponentID(busiComponent.getID());
      vmObj.setCreateTime(busiComponent
        .getCreateTime() == null ? new UFDateTime(System.currentTimeMillis()).toString() : busiComponent
        .getCreateTime().toString());
      vmObj.setDeleteOperateID(getOperId(busiComponent, "delete"));
      vmObj.setDisableOperateID(getOperId(busiComponent, "disable"));
      vmObj.setEditOperatID(getOperId(busiComponent, "edit"));
      vmObj.setEnableOperateID(getOperId(busiComponent, "enable"));
      vmObj.setAddOperateID(getOperId(busiComponent, "add"));
      String oldVersion = busiComponent.getVersion();
      vmObj.setVersion(String.valueOf(Integer.valueOf(oldVersion == null ? "1" : oldVersion).intValue() + 1));
    }
    defdoclist.setBpfcomponentid(vmObj.getComponentID());
    return vmObj;
  }

  private static String getOperId(IComponent busiComponent, String operCode)
  {
    String operId = null;
    if (busiComponent != null) {
      IBusiOperation oper = busiComponent.getBusiOperationByName(operCode);
      if (oper != null) {
        operId = oper.getID();
      }
    }
    if (StringUtils.isBlank(operId)) {
      operId = UUIDGenerator.getInstance().generateTimeBasedUUID().toString();
    }
    return operId;
  }

  private static String generateBpfFile(DefdoclistVO defdoclist, DefdocBpfVmObject boVmObj) throws BusinessException {
    String nchome = RuntimeEnv.getInstance().getNCHome();
    VelocityUnSingletonCodeGenerator generator = new VelocityUnSingletonCodeGenerator(nchome + "/resources/nc/ui/bbd/defdoc/");

    String destPath = getDefdocBpfFileName(defdoclist);

    if (defdoclist.getDoclevel().intValue() == 1)
      return null;
    try
    {
      generator.generate(destPath, "DefdocBpfPublishTemplate.vm", boVmObj, "UTF-8");
    }
    catch (CodegenException e) {
      Logger.error(e.getMessage(), e);
      throw new BusinessException(e.getMessage(), e);
    }
    return destPath.toString();
  }
}